package com.codermn.test;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author jxau_scf
 * @date 2021/6/21 - 23:31
 */
public class MyProxy implements InvocationHandler {


    private HelloService helloService;
    public void setHelloService(HelloService helloService){
        this.helloService = helloService;
    }

    public <T> T getProxy(Class<T> clazz){
        return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class<?>[]{clazz}, this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("执行前 ");

        method.invoke(helloService, args);
        System.out.println("代理类执行后"+ method.getName()+"方法");

        return null;

    }
}
